package com.jingdianjichi.subject.application.controller;


import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: 标签controller
 * @return:
 * @Author:  sky
 * @date:  2024/11/7 21:30
 */
@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {

    @Autowired
    private SubjectLabelDomainService subjectLabelDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            // 判断是否启动info级别的日志，如果启动了再执行json转换，减少性能开销
            if(log.isInfoEnabled()){
                log.info("log-info:{}", JSON.toJSONString(subjectLabelDTO));
            }

            // 字符串需要同时检测， null 和 空串 两种情况，断言匹配false抛出异常
            Preconditions.checkArgument(!StringUtils.isBlank(subjectLabelDTO.getLabelName()),"不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDTOToBO(subjectLabelDTO);
            // alt + enter 快速生成返回变量
            Boolean result = subjectLabelDomainService.add(subjectLabelBO);
            return Result.ok(result);
        }catch (Exception e){
            log.error("log-error:{}",e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"标签id不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDTOToBO(subjectLabelDTO);
            Boolean result = subjectLabelDomainService.update(subjectLabelBO);

            return Result.ok(result);
        }catch (Exception e){
            return Result.fail("更新标签失败");
        }
    }

    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"标签id不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDTOToBO(subjectLabelDTO);
            Boolean result = subjectLabelDomainService.delete(subjectLabelBO);

            return Result.ok(result);
        }catch (Exception e){
            return Result.fail("更新分类失败");
        }
    }
    /**
     * @Description: 查询分类下的标签
     * @return:
     * @Author:  sky
     * @date:  2024/11/7 22:34
     */
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(),"分类id不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDTOToBO(subjectLabelDTO);
            List<SubjectLabelBO> subjectLabelBOList = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);
            List<SubjectLabelDTO> subjectLabelDTOList = SubjectLabelDTOConverter.INSTANCE.convertBoListToDTOList(subjectLabelBOList);
            return Result.ok(subjectLabelDTOList);
        }catch (Exception e){
            return Result.fail("查询分类下标签失败");
        }
    }
}
